#!/bin/bash

set -o errexit
set -o xtrace

test_dir="$(dirname $0)"
. $(dirname $0)/functions
src_dir="$(realpath ${test_dir}/..)"
IMAGE=$(echo "$IMAGE" | sed -e 's#percona/#perconalab/#')

build_operator() {
    if [ "${RELEASE:-1}" = 0 ]; then
        GO_LDFLAGS="-race"
    fi

    export IMAGE
    export GO_LDFLAGS="-w -s -trimpath $GO_LDFLAGS"
    pushd ${src_dir}
        docker build \
            --build-arg GIT_COMMIT=$GIT_COMMIT \
            --build-arg GIT_BRANCH=$GIT_BRANCH \
            --build-arg GO_LDFLAGS="$GO_LDFLAGS" \
            --squash \
            --no-cache \
            -t "${IMAGE}" -f build/Dockerfile .
    popd
    docker push ${IMAGE}
}

if [[ $BUILD = "0" ]]; then
    exit 0
fi

until docker ps;
    do sleep 1;
done

build_operator
